package cn.wzg.fragmentsample;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;

public class MainActivity extends AppCompatActivity implements TitleFragment.OnItemClick {
    boolean dualPane; // 是否在一屏上同时显示列表和详细内容
    int curCheckPosition = 0; // 当前选择的索引位置
    //TitleFragment titleFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        //读取保存的数据值
        if (savedInstanceState != null) {
            curCheckPosition = savedInstanceState.getInt("cur", 0); // 更新当前选择的索引位置
        }
        // 获取布局文件中添加的fragment
        View fragTitle = findViewById(R.id.frag_title);
        // 判断是否在一屏上同时显示列表和详细内容
        dualPane = fragTitle != null;
        if (dualPane) { // 如果在一屏上同时显示列表和详细内容
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fl_detail,DetailFragment.newInstance(
                            Data.TITLES[curCheckPosition],Data.DETAIL[curCheckPosition]))
                    .commit();
        }else {
            TitleFragment titleFragment=new TitleFragment();
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fl_detail,titleFragment)
                    .commit();
        }
    }

    @Override
    public void onAttachFragment(@NonNull Fragment fragment) {
        //super.onAttachFragment(fragment);
        if (fragment instanceof TitleFragment) {
            TitleFragment titleFragment = (TitleFragment) fragment;
            titleFragment.setOnItemClick(this);
        }
    }

    @Override
    public void onTitleSelected(int position) {
        curCheckPosition=position;
        if (dualPane) { // 如果在一屏上同时显示列表和详细内容
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fl_detail,DetailFragment.newInstance(
                            Data.TITLES[position],Data.DETAIL[position]))
                    .commit();
        }else {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fl_detail,DetailFragment.newInstance(
                            Data.TITLES[position],Data.DETAIL[position]))
                    .addToBackStack(null)
                    .commit();
        }
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        //屏幕旋转时，会造成变量值丢失，为了避免丢失，销毁前保存一下
        outState.putInt("cur",curCheckPosition);
    }
}